// pages/coachInfoWrite/coachInfoWrite.js
let http=require('../../utils/request.js')
const filter = require('../../utils/filter.js');
Page(filter.loginCheck({

  /**
   * 页面的初始数据
   */
  data: {
    name: '教练报名申请',
    payPrice: '80.00',
    maskFlag: true,
    popupViewPay: true,
    // popupViewEnroll: true,
    pics: [], //图片
    imgAll: [], //返回合集
    projectNum:0,
    projectIndex:0,
    projectArray: [],
  },
  // 擅长项目选择
  projectChange: function (e) {
    let _this=this
    let projectid=_this.data.projectTmp[e.detail.value].id
    this.setData({
      projectIndex: e.detail.value,
      projectNum: 1,
      projectid
    })
  },

  // 校区选择
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  // 显示遮罩及支付弹框
  showPayPopup(e) {
    let _this=this
    let nameInput=_this.data.nameInput
    let ageInput=_this.data.ageInput
    let idInput=_this.data.idInput
    let phoneInput=_this.data.phoneInput
    let bankInput=_this.data.bankInput
    let imgAll=_this.data.imgAll.toString()
    let project_id=_this.data.projectid
    http.postHttp('/user_coach_apply',{
      name:nameInput,
      age:ageInput,
      card:idInput,
      phone:phoneInput,
      bank:bankInput,
      coach_images:imgAll,
      project_id:project_id
    },{
      token:wx.getStorageSync('token')
    }).then(res=>{
      if(res.data.code==1){
        this.setData({
          maskFlag: false,
          popupViewPay: false
        })
      }else{
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })
      }
      console.log(res,'申请返回res')
    })


    
  },
  showPaySuccess(e) {                                                                                                         
    this.setData({
      maskFlag: false,
      popupViewPay: true,
      popupViewEnroll: false
    })
  },
  goBackIndex(e) {
    this.setData({
      maskFlag: true,
      popupViewPay: true,
      // popupViewEnroll: true
    })
    wx.navigateTo({
      url: '/pages/myIndex/myIndex',
    })
  },
  closePopop(e) {
    this.setData({
      maskFlag: true,
      popupViewPay: true,
      popupViewEnroll: true
    })
  },

  // input监听
  nameInput(e) {
    this.setData({
      nameInput: e.detail.value
    })
  },
  ageInput(e) {
    this.setData({
      ageInput: e.detail.value
    })
  },
  idInput(e) {
    this.setData({
      idInput: e.detail.value
    })
  },
  phoneInput(e) {
    this.setData({
      phoneInput: e.detail.value
    })
  },
  bankInput(e) {
    this.setData({
      bankInput: e.detail.value
    })
  },


  //图片放大查看
  previewImg: function (e) {

    var index = e.target.dataset.index; //当前图片地址
    var imgArr = e.target.dataset.list; //所有要预览的图片的地址集合 数组形式
    console.log(index, imgArr)
    wx.previewImage({
      current: imgArr[index],
      urls: imgArr,
    })
  },
  //上传图片
  chooseImg: function (e) {
    var that = this,
      pics = this.data.pics;
    console.log(pics);
    if (pics.length < 3) {
      wx.chooseImage({
        count: 3, // 最多可以选择的图片张数，默认9
        sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
        sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
        success: function (res) {

          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          // wx.showToast({
          //   title: '正在上传...',
          //   icon: 'loading',
          //   mask: true,
          //   duration: 10000
          // });  
          for (var i = 0; i < tempFilePaths.length; i++) {
            pics.push(tempFilePaths[i]);
          }
          console.log(pics);
          console.log(res)
          that.setData({
            pics: pics
          })
          wx.uploadFile({
            filePath: tempFilePaths[0],
            name: 'file',
            url: 'https://system.wm69.mintennet.com//upload_img',
            header: {
              "Content-Type": "multipart/form-data",
              token: wx.getStorageSync('token')
            },
            success(res) {

              console.log('sucUp', res)
              console.log(JSON.parse(res.data).data.url, 'cos-da')
              let commentImg = JSON.parse(res.data).data.url

              let imgAll = that.data.imgAll
              imgAll.push(commentImg)
              console.log(imgAll, 'imgAll')
              that.setData({
                commentImg,
                imgAll
              })
            }
          })
        },
      });
    } else {
      wx.showToast({
        title: '最多上传3张图片',
        icon: 'none',
        duration: 3000
      });
    }
  },
  // 删除图片
  deleteImg: function (e) {
    var that = this;
    var pics = this.data.pics;
    var index = e.currentTarget.dataset.index;
    pics.splice(index, 1);
    console.log(pics)
    this.setData({
      pics: pics,
    })
  },
  // 预览图片
  previewImg1: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var pics = this.data.pics;
    wx.previewImage({
      //当前显示图片
      current: pics[index],
      //所有图片
      urls: pics
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 请求项目列表
    http.getHttp('/coach_projects',{},{}).then(res=>{
      console.log('项目列表',res)
      this.setData({
        projectArray:res.data.data,
        projectTmp:res.data.data,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))